package org.kjtc.service;

import net.sf.json.JSONObject;
import org.kjtc.commom.CommonUtil;
import org.kjtc.commom.XmlSerializer;
import org.kjtc.entity.*;
import org.kjtc.exception.UpdateRuntimeException;
import org.kjtc.mapper.BackLogMapper;
import org.kjtc.mapper.TimerMapper;
import org.kjtc.web.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/21 0021.
 */
@Service
public class BackLogService{

    @Autowired
    private BackLogMapper backLogMapper;

    @Autowired
    private TimerMapper timerMapper;

    public List<Product> getAllProducts() {
        List<Product> lists=backLogMapper.getAllProducts();
        return lists;
    }
    public List<ProductVersion> getAllProductVersions(String productId) {
        List<ProductVersion> lists=backLogMapper.getAllProductVersions(productId);
        return lists;
    }
    public List<UserInfo> getAllMembers() {
        List<UserInfo> lists=backLogMapper.getAllMembers();
        return lists;
    }
    public List<BackLog> getBackLog(String productVersionId, String MemberId, String BacklogStatus) {
        List<BackLog> lists=backLogMapper.getBackLog(productVersionId,MemberId,BacklogStatus);
        return lists;
    }
    public List<Map<String,Object>> getBackLogFeed(String backLogId) {
        List<Map<String,Object>> list=backLogMapper.getBackLogFeed(backLogId);
        return list;
    }
    public int updateActualTime(String backLogID) {
       int number=backLogMapper.updateActualTime(backLogID);
        if(number!=1){
           throw new RuntimeException("打卡失败");
        }
        return number;
    }
    public int getUserIDByBackLogID(String userId,String backLogId){
        List<BackLog> list=backLogMapper.getUserIDByBackLogID(backLogId);
        int num=0;
        if(list.get(0).getUserID().equals(userId)){
            num=1;
        }
        return num;
    }
    public int getPunchEveryDay(String userId,String backLogId){
        List<PunchLog> lists=backLogMapper.getPunchEveryDay(userId);
        int num=0;
        for(PunchLog p:lists){
            if(p.getBackLogID().equals(backLogId)){
                num=1;
            }
        }
        return num;
    }
    public List<Map<String,Object>> getFeedByProductVersionId(String productVersionId){
        List<Map<String,Object>> lists=backLogMapper.getFeedByProductVersionId(productVersionId);
        return lists;
    }
    public List<BackLog> getBackLogByProductVersionId(String productVersionId){
        List<BackLog> lists=backLogMapper.getBackLogProductVersionId(productVersionId);
        return lists;
    }
    public List<BackLog> getBackLogByUser(BackLog backLog){
        List<BackLog> lists=backLogMapper.getBackLogByUser(backLog);
        return lists;
    }
    public int addPunchRecord(String userName,String backLogId){
        int number=backLogMapper.addPunchRecord(userName,backLogId);
        if(number!=1){
            throw new RuntimeException("打卡数据未更新");
        }
        return number;
    }
    public int updateBackLogState(String backLogState,String backLogID,String userID){
        int number=backLogMapper.updateBackLogState(backLogState,backLogID,userID);
        if(number!=1){
            throw new UpdateRuntimeException("更新元素状态失败");
        }
        return number;
    }
    public int updateBackLogEndDTTM(String backLogState,String backLogID,String userID){
        int number=backLogMapper.updateBackLogEndDTTM(backLogState,backLogID,userID);
        if(number!=1){
            throw new UpdateRuntimeException("更新元素状态失败");
        }
        return number;
    }
    public int updateBackLogUser(String backLogID,String UserID,String updateUser){
        int number=backLogMapper.updateBackLogUser(backLogID,UserID,updateUser);
        if(number!=1){
            throw new UpdateRuntimeException("更新backLog成员失败");
        }
        return number;
    }
    public List<BackLog> getCurrentTitle(String productVersionId) {
        List<BackLog> lists=backLogMapper.getCurrentTitle(productVersionId);
        return lists;
    }
    public void sendWorkDataByUserID(String UserID) {

        System.out.println("进入"+UserID+"推送工作任务");

        File file = new File(CommonUtil.WECHAT_CONFIG_PATH);
        int access = 0;
        String forwarUrl = "";
        String pushUrl = "";
        String wechatModuleID = "";
        String statisticsTime = "";
        if (file.exists()) {
            try {
                XmlSerializer<WeChatConfig> xSerializer = new XmlSerializer<WeChatConfig>();
                access = xSerializer.unMarshallXml(file, WeChatConfig.class).getAccess();
                forwarUrl = xSerializer.unMarshallXml(file, WeChatConfig.class).getForwardUrl();
                pushUrl = xSerializer.unMarshallXml(file, WeChatConfig.class).getPushUrl();
                wechatModuleID = xSerializer.unMarshallXml(file, WeChatConfig.class).getWeChatModuleID();
                statisticsTime = xSerializer.unMarshallXml(file, WeChatConfig.class).getStatisticsTime();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (access == 1) {
            List<BackLog> list=timerMapper.getAllWorkDataByUserID(UserID);
                if (list.size() > 0) {
                    Map<String, Object> dataMap = new HashMap<String, Object>();
                    Map<String, Object> requestMap = new HashMap();
                    dataMap.put("first", "{value: \"" + list.get(0).getUserName() + "\", color: \"#FF0000\"}");
                    String keyWord0 = "";
                    String userID = "";
                    for (int i = 0;i < list.size();i++) {
                        BackLog backLog = list.get(i);
                        userID = backLog.getUserID();
                        String tmp = "";
                        if(i == 0){
                            tmp = "\n"+ backLog.getProductName() + "-" + backLog.getBackLogName() + "\n";
                        }else if (i == list.size() - 1) {
                            tmp = backLog.getProductName() + "-" + backLog.getBackLogName();
                        } else {
                            tmp = backLog.getProductName() + "-" + backLog.getBackLogName() + "\n";
                        }
                        keyWord0 += tmp;
                    }
                    JSONObject json = new JSONObject();
                    json.put("value", keyWord0);
                    json.put("color", "#0000FF");
                    dataMap.put("keyword1", json);
                    JSONObject json1 = new JSONObject();
                    json1.put("value", "项目管理");
                    json1.put("color", "#0000FF");
                    dataMap.put("keyword2", json1);
                    JSONObject json2 = new JSONObject();
                    json2.put("value", "PM");
                    json2.put("color", "#0000FF");
                    dataMap.put("keyword3", json2);
                    requestMap.put("sendFrom", "项目管理");
                    requestMap.put("url", forwarUrl);
                    requestMap.put("templateID", wechatModuleID);
                    requestMap.put("userID","'" + userID+ "'");
                    requestMap.put("data", dataMap);
                    try{
                        Tools.requestHttpUrl(pushUrl, requestMap);
                    }catch (Exception e){
                        e.printStackTrace();
               }
            }
        }

    }


}
